package com.group01.community.utils;

import com.group01.community.entity.SysDictType;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.usermodel.FillPatternType;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

public class POI {
    /**
     * 处理excel
     *
     * @return
     */
    public static ResponseEntity<byte[]> handleExcel(List<SysDictType> dictTypes) {
        //excel文档
        HSSFWorkbook workBook = new HSSFWorkbook();
        //创建sheet
        HSSFSheet sheet = workBook.createSheet("字典类型信息");
        //表头样式
        HSSFCellStyle styleHeader = workBook.createCellStyle();
        styleHeader.setFillForegroundColor(IndexedColors.GREY_40_PERCENT.index);
        styleHeader.setAlignment(HorizontalAlignment.CENTER);
        styleHeader.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        HSSFCellStyle style = workBook.createCellStyle();
        style.setAlignment(HorizontalAlignment.CENTER);
        sheet.setColumnWidth(0, 10 * 256);
        sheet.setColumnWidth(1, 10 * 256);
        sheet.setColumnWidth(2, 20 * 256);
        sheet.setColumnWidth(3, 10 * 256);
        sheet.setColumnWidth(4, 15 * 256);
        sheet.setColumnWidth(5, 20 * 256);

        //表头
        HSSFRow r0 = sheet.createRow(0);

        HSSFCell c0 = r0.createCell(0);
        c0.setCellValue("字典编号");
        c0.setCellStyle(styleHeader);

        HSSFCell c1 = r0.createCell(1);
        c1.setCellValue("字典名称");
        c1.setCellStyle(styleHeader);

        HSSFCell c2 = r0.createCell(2);
        c2.setCellValue("字典类型");
        c2.setCellStyle(styleHeader);

        HSSFCell c3 = r0.createCell(3);
        c3.setCellValue("状态");
        c3.setCellStyle(styleHeader);

        HSSFCell c4 = r0.createCell(4);
        c4.setCellValue("备注");
        c4.setCellStyle(styleHeader);

        HSSFCell c5 = r0.createCell(5);
        c5.setCellValue("创建时间");
        c5.setCellStyle(styleHeader);

        //表体
        for (int i = 0; i < dictTypes.size(); i++) {
            HSSFRow row = sheet.createRow(i + 1);
            SysDictType dictType = dictTypes.get(i);

            HSSFCell cell0 = row.createCell(0);
            cell0.setCellStyle(style);
            cell0.setCellValue(dictType.getDictId());

            HSSFCell cell1 = row.createCell(1);
            cell1.setCellStyle(style);
            cell1.setCellValue(dictType.getDictName());

            HSSFCell cell2 = row.createCell(2);
            cell2.setCellStyle(style);
            cell2.setCellValue(dictType.getDictType());

            HSSFCell cell3 = row.createCell(3);
            cell3.setCellStyle(style);
            cell3.setCellValue(dictType.getStatus());

            HSSFCell cell4 = row.createCell(4);
            cell4.setCellStyle(style);
            cell4.setCellValue(dictType.getRemark());

            HSSFCell cell5 = row.createCell(5);
            cell5.setCellStyle(style);
            cell5.setCellValue(dictType.getCreateTime());
        }

        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        HttpHeaders httpHeaders = new HttpHeaders();
        try {
            String s = UUID.randomUUID().toString();
            s = s.substring(0, s.indexOf("-")) + ".xlsx";
            httpHeaders.setContentDispositionFormData("attachment", "type_" + s);
            httpHeaders.setContentType(MediaType.APPLICATION_OCTET_STREAM);
            workBook.write(baos);
        } catch (IOException e) {
            e.printStackTrace();
        }

        return new ResponseEntity<>(baos.toByteArray(), httpHeaders, HttpStatus.CREATED);
    }
}
